package cn.detu.ln.service.impl;

import cn.detu.ln.entity.Admin;
import cn.detu.ln.mapper.AdminMapper;
import cn.detu.ln.service.AdminService;
import cn.detu.ln.utils.RestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public RestUtil selectAll(){
        // select * from user
        return new RestUtil(200,"",adminMapper.selectList(null));// 查询数据库中user表所有数据
    }

    @Override
    public Admin queryAdminById(Integer adminId) {
        // 查询单条数据使用selectOne，new QueryWrapper相当于创建查询条件  where admin_id = adminId
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("admin_id",adminId));
    }

    @Override
    public RestUtil adminDelete(Integer adminId) {
        return adminMapper.deleteById(adminId)>0? new RestUtil(200,"删除成功",null):new RestUtil(500,"删除失败",null);
    }

    @Override
    public RestUtil adminUpdate(Admin admin) {
        return adminMapper.updateById(admin)>0?new RestUtil(200,"修改成功",null):new RestUtil(500,"修改失败",null);
    }

    @Override
    public RestUtil adminInsert(Admin admin) {
        return adminMapper.insert(admin)>0?new RestUtil(200,"添加成功",null):new RestUtil(500,"添加失败",null);
    }

    @Override
    public RestUtil queryAdminByPage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Admin> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Admin>().like("admin_name",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Admin> adminPage = adminMapper.selectPage(new Page<Admin>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",adminPage.getTotal());// 总条数
        map.put("pages",adminPage.getPages());// 总页数
        map.put("admins",adminPage.getRecords());// 当前页数据
        return adminPage != null ? new RestUtil(200,"",map) : null;
    }

    @Override
    public RestUtil login(Admin admin) {
        // 根据用户名查询Admin
        Admin resUser = adminMapper.selectOne(new QueryWrapper<Admin>().eq("admin_name",admin.getAdminName()));
        if(resUser == null ) return new RestUtil(500,"用户名不存在，请先注册！",null);
        // 用户名存在，判断密码是否正确
        return resUser.getAdminPwd().equals(admin.getAdminPwd()) ?
                new RestUtil(200,"恭喜，登录成功！",null) :
                new RestUtil(500,"密码错误，请重试！",null);
    }

    @Override
    // 注册
    public RestUtil regist(Admin admin) {
        Admin resAdmin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("admin_name", admin.getAdminName()));

        if (resAdmin != null) return new RestUtil(500,"账户名已存在，请重试！",null);
        return adminMapper.insert(admin) > 0 ? new RestUtil(200,"注册成功",null) : new RestUtil(500,"注册失败",null);
    }

    @Override
    public RestUtil adminUpdatePwd(Admin admin,String newPwd) {
        // 判断新密码与旧密码不能一样
        System.out.println(admin.getAdminPwd());
        System.out.println(newPwd);
        if(admin.getAdminPwd().equals(newPwd))  return new RestUtil(500,"新密码与旧密码不能一样，请重试！",null);
        admin.setAdminPwd(newPwd);
        return adminMapper.updateById(admin) >0 ?new RestUtil(200,"恭喜，密码已修改成功！",null) : new RestUtil(500,"密码修改失败，请重试！",
                null);
    }

    @Override
    public RestUtil select(Integer adminId) {
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("admin_id",adminId));
        if(admin == null) return new RestUtil(500,"用户不存在！",null);
        return new RestUtil(200,"查询成功！",admin);
    }
}
